package pers.duorhs.common.controller;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.duorhs.common.socket.SocketClientHandler;
import pers.duorhs.common.socket.SocketServerHandler;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Duorhs
 * @date 2022/5/7 14:08
 */
@RestController
@Tag(name = "SocketController", description = "socket")
@RequestMapping("socket")
@AllArgsConstructor
public class SocketController {
    private final SocketClientHandler clientHandler;
    private final SocketServerHandler serverHandler;
    private final SocketIOServer socketIOServer;

    @PostMapping("/login")
    public void login(@RequestParam String name, @RequestParam String room) {
        clientHandler.login(name, room);
    }

    @PostMapping("/logout")
    public void logout(@RequestParam String name) {
        serverHandler.logout(name);
    }

    @PostMapping("allClients")
    public List<String> allClients() {
        return serverHandler.getClients().stream().map(serverHandler::getUser).collect(Collectors.toList());
    }

    @PostMapping("allClientsFromRoom")
    public List<String> allClientsFromRoom(@RequestParam String room) {
        return serverHandler.getClientsFromRoom(room).stream().map(serverHandler::getUser).collect(Collectors.toList());
    }

    @PostMapping("allClientsMap")
    public LinkedHashMap<String, SocketIOClient> allClientsMap() {
        return serverHandler.getClientsMap();
    }

    @PostMapping("send")
    public void send(@RequestParam String client, @RequestParam String msg) {
        serverHandler.sendMsg(client, msg);
    }

    @PostMapping("sendToRoom")
    public void sendToRoom(@RequestParam String room, @RequestParam String msg) {
        serverHandler.sendMsgToRoom(room, msg);
    }

    @PostMapping("debug")
    public void debug() {

        System.out.println();
    }
}
